﻿#include "C3fdGeomWidget.h"
#include "MainWindow/MainWindow.h"
#include <QTreeWidgetItem>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include "ConfigOptions/ConfigOptions.h"
#include "ConfigOptions/GeometryConfig.h"
#include "Settings/BusAPI.h"
#include "GeometryWidgets/geometryDialogFactory.h"
#include <QFileDialog>

#include <QMessageBox>
#include "Common/C3fdProject.h"

namespace MainWidget
{
	C3fdGeomWidget::C3fdGeomWidget(GUI::MainWindow *m, QWidget *parent)
		: QTreeWidget(parent)
	{
		_mainWindow = m;
		this->setHeaderHidden(true);
		_root = new QTreeWidgetItem(this, 0);
		_root->setText(0, tr("Geometry"));
		_root->setIcon(0, QIcon(":/QUI/icon/geometry.png"));
		this->addTopLevelItem(_root);
		_root->setExpanded(true);

		setStyleSheet("QTreeWidget::item{height:20px;}");
		connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(itemClickedSlot(QTreeWidgetItem *, int)));
		connect(this, SIGNAL(renderGeomSig(QStringList, bool)), m, SIGNAL(renderC3fdGeomSig(QStringList, bool)));
	}

	C3fdGeomWidget::~C3fdGeomWidget()
	{
		if (_root != nullptr)
			delete _root;
	}

	void C3fdGeomWidget::itemClickedSlot(QTreeWidgetItem *item, int)
	{
		if (item == nullptr || item == _root) {
			return;
		}
		auto isShow = item->checkState(0) == Qt::CheckState::Checked ? true : false;
		emit updateDisplayStatus(item->text(0), isShow);
	}

	void C3fdGeomWidget::contextMenuEvent(QContextMenuEvent *event)
	{
		Q_UNUSED(event);
		_currentItem = currentItem();
		if (!_currentItem)
		{
			return;
		}
		if (_currentItem->type() == 0)
		{
			QMenu pop_menu;
			QAction *action = nullptr;

			ConfigOption::ConfigOption *option = ConfigOption::ConfigOption::getInstance();
			ConfigOption::GeometryConfig *geoconfig = option->getGeometryConfig();

			bool isimport = geoconfig->isImportGeometryEnabled();
			if (isimport)
			{
				action = pop_menu.addAction(QIcon(), tr("Import Geometry"));
				connect(action, SIGNAL(triggered()), this, SLOT(importGeomtrySlot()));

				/*action = pop_menu.addAction(QIcon(), tr("Hide All"));
				connect(action, SIGNAL(triggered()), this, SLOT(hideAll()));

				action = pop_menu.addAction(QIcon(), tr("Show All"));
				connect(action, SIGNAL(triggered()), this, SLOT(showAll()));

				action = pop_menu.addAction(QIcon(), tr("Remove All"));
				connect(action, SIGNAL(triggered()), this, SLOT(removeAllGeometery()));

				action = pop_menu.addAction(QIcon(), tr("Remove Visible"));
				connect(action, SIGNAL(triggered()), this, SLOT(removeVisibleGeometery()));*/
			}

			pop_menu.exec(QCursor::pos());
		}
	}

	void C3fdGeomWidget::importGeomtrySlot()
	{
		QStringList files = QFileDialog::getOpenFileNames(this, tr("Select geomTurbo files"), "", tr("Geometry File (*.geomTurbo)"));
		if (files.isEmpty())
		{
			return;
		}
		emit renderGeomSig(files, _root->childCount() == 0);
	}

	void C3fdGeomWidget::updateC3fdRowTree() {
		_root->takeChildren();
		auto* currentCase = Common::C3fdProject::instance()->getCurrentCase();
		if (currentCase != nullptr) {
			auto geoms = currentCase->getGeomTurboFiles();
			if (geoms.empty()) {
				return;
			}
			for (auto* geom : geoms)
			{
				for (const auto& part : geom->getParts()) {
					auto* node = new QTreeWidgetItem(_root, 1);
					node->setCheckState(0, Qt::CheckState::Checked);
					node->setText(0, QString("%1-%2").arg(geom->getFileName()).arg(part));
					node->setIcon(0, QIcon(":/QUI/icon/geometry.png"));
				}
			}
		}
	}

}
